package com.campus.shop.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.shop.common.ConstantProperties;
import com.campus.shop.common.Result;
import com.campus.shop.controller.web.vo.GoodsDetailVo;
import com.campus.shop.controller.web.vo.GoodsListVo;
import com.campus.shop.controller.web.vo.SaleUserInfoVo;
import com.campus.shop.entity.Goods;
import com.campus.shop.entity.GoodsCategory;
import com.campus.shop.mapper.GoodsMapper;
import com.campus.shop.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 商品控制器
 * @since 2023-12-26 14:22:52
 */
@RestController
@RequestMapping("/web/goods/")
@Slf4j
public class GoodsController extends BaseWebController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsMapper goodsMapper;

    //获取商品分类
    @PostMapping("/get-goods-category")
    public Result getGoodCategory() {
        List<GoodsCategory> list = goodsService.goodCateList();
        return Result.success(list);
    }

    //获取商品分页列表
    @PostMapping("/get-goods")
    public Result getGoods(@RequestParam("page") Integer page, Integer cateId, String goodsName) {
        HashMap<String, Object> res = goodsService.pageList(page, cateId, goodsName);
        return Result.success(res);
    }

    //获取商品详情
    @PostMapping("/get-goods-detail")
    public Result getGoodsDetail(@RequestParam("goodsId") Integer goodsId) {
        GoodsDetailVo goodsInfo = goodsService.findGoodById(goodsId);
        goodsInfo.setGoodsImg(ConstantProperties.hostName + goodsInfo.getGoodsImg());
        SaleUserInfoVo userSaleInfo = goodsService.getGoodsSaleInfo(goodsInfo.getUserId());
        HashMap<String, Object> res = new HashMap<>();
        res.put("goodsInfo",goodsInfo);
        res.put("userSaleInfo",userSaleInfo);
        return Result.success(res);
    }

    //获取推荐商品
    @PostMapping("/get-suggest-goods")
    public Result getSuggestGoods(){
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.last("limit 8");
        List<Goods> goodList = goodsMapper.selectList(wrapper);
        ArrayList<GoodsListVo> list = new ArrayList<>();
        for (Goods goods : goodList){
            GoodsListVo goodsListVo = new GoodsListVo();
            goodsListVo.setId(goods.getId());
            goodsListVo.setPrice(goods.getPrice());
            goodsListVo.setGoodsImg(ConstantProperties.hostName + goods.getGoodsImg());
            goodsListVo.setGoodsName(goods.getGoodsName());
            goodsListVo.setRemark(goods.getRemark());
            list.add(goodsListVo);
        }
        return Result.success(list);
    }


}


